<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>文档</title>
  <link href="../css/semantic/semantic.min.css" rel="stylesheet">
  <link href="../css/markdown.css" rel="stylesheet">
    <link href="../css/wtxynavigation.css" rel="stylesheet">
    <script src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/include.js"></script>
  <style>
    body{padding-top:0;background:#f7f7f7}
    body>.ui.container{width:100%;padding:0}
    #project-title{background:#6e6e6e;width:100%;line-height:3.572rem;color:#fff;font-size:1.2rem}
    #project-title .title-wrap{font-weight:700;padding:0 12px;width:100%;max-width:1366px!important;min-width:900px;margin:0 auto}
    #project-title .left,#project-title right{display:inline-block}
    #project-title .right{float:right}
    #project-title .right .wiki-actions .menu{left:auto;right:0;margin-top:.4em!important}
    #wiki-preview-container{width:100%;max-width:1366px!important;min-width:900px;padding:0;border:1px solid #e3e9ed;border-top:0}
    #wiki-preview>.ui.segment{margin-top:0;padding:1rem;padding-left:0;border:0}
    .hover-places{display:inline-block}
    #page-detail{width:60%;margin:0 auto}
    #page-detail .title{font-weight:700;font-size:24px;padding:12px 0;border-bottom:1px solid #ccc}
    #page-detail .content{margin-top:15px}
    .content p{word-wrap:break-word}
    #wiki-parent,#wiki-title,#wikis{display:none}
    .ui.grid{padding:0!important}
    #ex-all{cursor:pointer;color:#537c8d}
  </style>
</head>
<body>
  <div class="ui container">

      <div id="wtxyNav"></div>

    <div class="ui container" id="wiki-preview-container">
      <div id="wiki-preview">
        <div class="ui segment">
          <div id="page-detail" class="markdown-body">
            <div class='title'>14.GCD异步并发</div><div class='content'><h1>
<a id="gcd异步并发" class="anchor" href="#gcd%E5%BC%82%E6%AD%A5%E5%B9%B6%E5%8F%91"></a>GCD异步并发</h1>
<h1>
<a id="下面代码打印结果是什么" class="anchor" href="#%E4%B8%8B%E9%9D%A2%E4%BB%A3%E7%A0%81%E6%89%93%E5%8D%B0%E7%BB%93%E6%9E%9C%E6%98%AF%E4%BB%80%E4%B9%88"></a>下面代码打印结果是什么?</h1>
<pre><code>
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        NSLog(@"1");
        
        [self performSelector:@selector(doSomething) withObject:nil afterDelay:0];
        
        NSLog(@"3");
    });
    
    
}



- (void)doSomething
{
    NSLog(@"2");
}


</code></pre>
<h2>
<a id="描述" class="anchor" href="#%E6%8F%8F%E8%BF%B0"></a>描述</h2>
<ol>
<li>异步+全局队列,这种方式, 实际上是在GCD底层所维护的线程池, 中的某一个线程中处理</li>
<li>GCD下面的这些线程, 默认是没有开启<code>runloop</code>的</li>
<li>
<code>performSelector: withObject: afterDelay:</code>这个方法, 要创建提交任务到<code>runloop</code>的逻辑</li>
<li>然后 GCD下面的这些线程没有<code>runloop</code>, 所以<code>performSelector: withObject: afterDelay:</code>这个方法就无效了</li>
<li>
<code>performSelector: withObject: afterDelay:</code>这个方法要想有效, 必须满足所处的线程有<code>runloop</code>. 没有的话就失效</li>
</ol>
<h1>
<a id="答--13" class="anchor" href="#%E7%AD%94--13"></a>答:  1,3</h1></div>
          </div>
        </div>
      </div>
      </div>
  </div>
</body>
</html>